/*
            /$$
    /$$    /$$$$
   | $$   |_  $$    /$$$$$$$
 /$$$$$$$$  | $$   /$$_____/
|__  $$__/  | $$  |  $$$$$$
   | $$     | $$   \____  $$
   |__/    /$$$$$$ /$$$$$$$/
          |______/|_______/
================================
        Keep calm and get rich.
                    Is the best.

  	@Author: Dami
  	@Date:   2017-09-06 15:27:44
  	@Last Modified by:   Dami
  	@Last Modified time: 2019-04-26 21:39:28
*/

window.$ = jQuery;

if( nice_url.single == 1 ){
	jQuery(document).ready(function($) {
	    $('.post-social').theiaStickySidebar({
	        additionalMarginTop: 33,
	        additionalMarginBottom: 200,
	        defaultPosition: 'absolute'
	    });
	});
}

function toggleCommentAuthorInfo() {
    var changeMsg = '<i class="text-md iconfont icon-edit-outline"></i>';
    var closeMsg = '<i class="text-md iconfont icon-chevron-up-outline"></i>';
    $('.comment-form-info').slideToggle('slow', function(){
        if ( $('.comment-form-info').css('display') == 'none' ) {
            $('#toggle-comment-author-info').html(changeMsg);
        } else {
            $('#toggle-comment-author-info').html(closeMsg);
        }
    });
};
/*
	Sticky Menu
	----------------------------------------------------

*/
function scrollTop() {
    var $window           = $(window),
    	$window_width    = $window.width(),
    	$window_height    = $window.height(),
    	scroll            = $window.scrollTop(),
        startPoint        = $window_height / 2,
        scrTopBtn         = $("#nice-back-to-top");
    if ( scroll >= startPoint && $window_width >= 1024  ) {
        scrTopBtn.addClass('active');
    } else {
        scrTopBtn.removeClass('active');
    }
    scrTopBtn.on("click", function () {
        $("html, body").stop().animate({
            scrollTop: 0
        },
        600);
    });
};
jQuery(document).scroll(function ($) {
    scrollTop()
});

jQuery(document).ready(function($) {
    $('[data-toggle="tooltip"]').tooltip();
    //menu
    $('.mobile-menu .menu-item-has-children').prepend('<span class="menu-icon"><i class="iconfont icon-more-horizontal-outl"></i></span>');
    $('.mobile-menu .menu-icon').on('click',null,function() {
        var $submenu = $(this).closest('.menu-item-has-children').find(' > .sub-menu');
        $submenu.slideToggle(500);
    	return false;
    });

    /*$('.search-input input').on('input', function(e) {
    var $input = jQuery(this);
    var query = $input.val();
    jQuery.ajax({
        url: globals.ajax_url,
        type : 'post',
        data : {
            action : 'load_search_results',
            query : query
        },
        error: function(response) {
            // console.log(response)
        },
        success: function(response) {
            if (query === '') response = '';
            var ulNode = jQuery(".search-result ul");
            var loadMoreNode = jQuery(".search-result .loadmore");
            ulNode.html(response);
            var items = ulNode.find("li") || [];
            jQuery(".widget-search .count").html('(' + items.length + ')');
            if (items.length < 5) {
                loadMoreNode.hide()
            } else {
                loadMoreNode.show()
                loadMoreNode.find('a').attr('href' , globals.site_url + '/?s=' + query);
            }
        }
    });
    return false;
    });*/
});

$(document).on('click', '.action-menu', function(event) {
	event.preventDefault();
    $('.mobile-navbar').toggleClass('active');
    $('body').toggleClass('active');
});

$(document).on('click', '.bg-overlay', function(event) {
	event.preventDefault();
    $('.mobile-navbar').removeClass('active');
    $('.sidebar-collapse').removeClass('active');
    $('body').removeClass('active-sidebar').removeClass('active');

});

$(document).on('click', '.action-search', function(event) {
	event.preventDefault();
	$('.sidebar-collapse').toggleClass('active');
    $('body').toggleClass('active-sidebar');
});

function ajax_load_comments( data ){
    var buttonDOM = $('#comments-next-button');
    buttonDOM.hide();

	$.ajax({
		url: globals.ajax_url,
		type: 'POST',
		dataType: 'html',
		data: data,
	})
	.done(function( response ) {
		if( response ){
			if( data.commentspage == 'newest' ){
				buttonDOM.data( 'paged', data.paged*1-1);
			}else{
				buttonDOM.data( 'paged', data.paged*1+1);
			}
			$('.'+data.append).append(response);
			buttonDOM.show();
		} else {
            buttonDOM.hide();
		}

	})
}

$(document).on('click', '#comments-next-button', function(event) {
    event.preventDefault();
    ajax_load_comments($('#comments-next-button').data());
});

function menu_item_hidden(){
	var right = jQuery('.navbar').width() + jQuery('.navbar').offset().left;


	if( right < jQuery('.main-menu > li:nth-last-child(-n+1)').offset().left + 150 ){
		var i = 1;
		while ( true ) {
			var hiddenMenus = jQuery('.main-menu > li:nth-last-child(-n+'+i+')');
			if( hiddenMenus.offset().left + 150 <  right ){
				hiddenMenus.remove();
				break;
			}
			i++;
		}
		var collapseMenus = '<li class="menu-item"><a href="#"><i class="text-lg text-primary iconfont icon-more-vertical-outlin"></i></a>'
			+ '<ul class="sub-menu">'
			+ $("<div/>").append(hiddenMenus.clone()).html()
			+ '</ul>'
		+ '</li>';
		jQuery('.main-menu').append(collapseMenus);
	}
}


jQuery(window).bind("load", function() {

	menu_item_hidden();
	return true;

});

jQuery(window).on('resizeend', function(e) {

	var navbarSite = jQuery('.navbar-site');
	navbarSite.append(jQuery('.hidden-nav .sub-menu li').clone());
	jQuery('.hidden-nav').remove();

	menu_item_hidden();

});

$(document).on("click", '.post-like[data-action="like"]', function() {
	event.preventDefault();
	var $this = $(this);
	var id = $this.data("id");

	if( $this.hasClass('requesting') ){
		return false;
	}

	$this.addClass('requesting');
	$.ajax({
		url: globals.ajax_url,
		type: 'POST',
		dataType: 'html',
		data: { action: 'suxing2019_like', id, like_action: 'like'},
	})
	.done(function( data ) {
		$this.addClass('current');
		$this.attr('data-action', 'unlike');
		ncPopupTips(1, '谢谢点赞')
		$('.like-count').html('(' + data + ')');
	})
	.always(function() {
		$this.removeClass('requesting');
	});
	return false;
});


$(document).on("click", '.post-like[data-action="unlike"]', function() {
	event.preventDefault();
	var $this = $(this);
	var id = $this.data("id");

	if( $this.hasClass('requesting') ){
		return false;
	}

	$this.addClass('requesting');

	$this.removeClass('current');

	$.ajax({
		url: globals.ajax_url,
		type: 'POST',
		dataType: 'html',
		data: { action: 'suxing2019_like', id, like_action: 'unlike'},
	})
	.done(function( data ) {
		$this.removeClass('current');
		$this.attr('data-action', 'like');
		ncPopupTips(0, "取消点赞")
		$('.like-count').html('(' + data + ')');
	})
	.always(function() {
		$this.removeClass('requesting');
	});
	return false;
});
$(document).on('click', '.single-popup', function(event) {
	event.preventDefault();
	var img = $(this).data("img");
	var title = $(this).data("title");
	var desc = $(this).data("desc");
	var html = '<div class="text-center"><h6 class="py-2">' + title + '</h6>\
				<img src="' + img + '" alt="' + title + '" style="width:100%;height:auto;">\
				<p class="text-muted text-xs">('+ desc +')</p></div>'
	ncPopup('small', html)
});

$(document).on('click', '.page-break-ajax', function(event) {
	event.preventDefault();

	var that = $(this);
	var href = $(this).data('href');

	$('.page-break-ajax-load .post-loading').show();
	$('.page-break-ajax-load button').hide();

	$.get(href, function(data) {

		var html = $(data).find('.content-body').html();

		$(html).insertBefore('div.page-break-ajax-load');

		if( typeof page_break[cursor_page] != 'undefined' ){
			that.data( 'href', page_break[cursor_page] );
			cursor_page++;
			$('.page-break-ajax-load .post-loading').hide();
			$('.page-break-ajax-load button').show();
		}else{
			$('.page-break-ajax-load .post-loading').hide();
			$('.page-break-ajax-load').hide();
		}

	});

});

console.log('\n' + ' %c Cosy Designed by nicetheme® %c https://www.nicetheme.cn ' + '\n', 'color: #fadfa3; background: #030307; padding:5px 0; font-size:18px;', 'background: #fadfa3; padding:5px 0; font-size:18px;');
